2. Variables and data types

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

Variables are used for storing values. Let the value be of any type , we need to store it somewhere so that we can

use it throughout the console/script. Variable names in PowerShell begin with a ** , as in _Variable1_ , and values are assigned using **= , like $Variable1 = "Value 1" .PowerShell supports a huge number of variable types; such as text strings, integers, decimals, arrays, and even advanced types like version numbers or IP addresses.

2.1: Simple variable

All variables in PowerShell begin with a US dollar sign ($). The simplest example of this is:

1
$foo = "bar"

This statement allocates a variable called foo with a string value of "bar".

2.2: Arrays

Array declaration in Powershell is almost the same as instantiating any other variable, i.e. you use a $name = syntax.

The items in the array are declared by separating them by commas(,):

1
2
$myArrayOfInts = 1 , 2 , 3 , 4
$myArrayOfStrings = "1","2","3","4"

Adding to an array

Adding to an array is as simple as using the + operator:

1
2
$myArrayOfInts = $myArrayOfInts + 5
# now contains 1,2,3,4 & 5!

Combining arrays together

Again this is as simple as using the + operator

1
2
3
4
$myArrayOfInts = 1 , 2 , 3 , 4
$myOtherArrayOfInts = 5 , 6 , 7
$myArrayOfInts = $myArrayOfInts + $myOtherArrayOfInts
# now 1,2,3,4,5,6,

2.3: List Assignment of Multiple Variables

Powershell allows multiple assignment of variables and treats almost everything like an array or list. This means that instead of doing something like this:

1
2
3
4
5
$input = "foo.bar.baz"
$parts = $input.Split(".")
$foo = $parts[ 0 ]
$bar = $parts[ 1 ]
$baz = $parts[ 2 ]

You can simply do this:

1
$foo, $bar, $baz = $input.Split(".")

Since Powershell treats assignments in this manner like lists, if there are more values in the list than items in your list of variables to assign them to, the last variable becomes an array of the remaining values. This means you can also do things like this:

1
2
3
$foo, $leftover = $input.Split(".") #Sets $foo = "foo", $leftover = ["bar","baz"]
$bar = $leftover[ 0 ] # $bar = "bar"
$baz = $leftover[ 1 ] # $baz = "baz"

2.4: Scope

The default scope for a variable is the enclosing container. If outside a script, or other container then the scope is Global. To specify a scope, it is prefixed to the variable name $scope:varname like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$foo = "Global Scope"
function myFunc {
  $foo = "Function (local) scope"
  Write-Host $global:foo
  Write-Host $local:foo
  Write-Host $foo
}
myFunc
Write-Host $local:foo
Write-Host $foo

Output:

Global Scope Function (local) scope Function (local) scope Global Scope Global Scope

2.5: Removing a variable

To remove a variable from memory, one can use the Remove-Item cmdlet. Note: The variable name does NOT include the $.

1
Remove-Item Variable:\foo

Variable has a provider to allow most *-item cmdlets to work much like file systems.

Another method to remove variable is to use Remove-Variable cmdlet and its alias rv

1
2
$var = "Some Variable" #Define variable 'var' containing the string 'Some Variable'
$var #For test and show string 'Some Variable' on the console
1
2
Remove-Variable -Name var
$var
1
2
#also can use alias 'rv'
rv var

2.6: Data types

While developing scripts, you may run into instances where you may want to force a specific data type. This is helpful in cases where PowerShell automatically interprets the output from a command incorrectly. You can force data types by the use of brackets specifying a data type and a variable.

There are a variety of data types that you can force within PowerShell. The following table represents the common data types for use with PowerShell and an explanation and an example of its use:

Data type Explanation Example $a value
[string] String of Unicode characters [string]$a = "Hello" Hello
[char] A Unicode 16-bit character [char]$a = 0xA9 ©
[byte] An 8-bit character [byte]$a = 0x0001D 29
[int] 32-bit integer [int]$a = 12345 12345
[long] 64-bit integer [long]$a = 1234.243 1234
[bool] Boolean True/False value [bool]$a = 1 True
[decimal] A 128-bit decimal [decimal]$a = 1234.243 1234.243
[single] A single-precision 32-bit number [single]$a = 1234.243 1234.243
[double] A double-precision 64-bit number [double]$a = 1234.243 1234.243
[datetime] A data time value [datetime]$a = "01-APR-2014" Tuesday, April 1, 2014 12:00:00 AM
[xml] A XML-styled value [xml]a = "<test><a>Testing</a></test>" <br />a.test.a Testing
[array] An array-styled value [array]$a = 1,2,3 1
2
3
[hashtable] A hashtable-styled value [hashtable]$a = @{"Old" = "New"} Name | Value
---------|---------
Old | New